package cn.zhouchao.controller;

import cn.zhouchao.block.UserBlock;
import cn.zhouchao.domain.User;
import cn.zhouchao.fallback.UserFallback;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope // 刷新配置文件
@RequestMapping("/user")
public class UserController {

//    @Value("${server.port}")
//    private String port;
//
//    @Value("${test.value}")
//    private String value;
//
//    @GetMapping(value = "/{id}")
//    public User getUser(@PathVariable Long id) {
//        return new User(id, "源码时代", "Java简单易学!");
//    }

    @Value("${server.port}")
    private String port;

    @Value("${test.value}")
    private String value;

    @GetMapping(value = "/{id}")
    @SentinelResource(value = "getUser",
            fallback = "getUserFallback",
            fallbackClass = UserFallback.class,
            blockHandler = "getUserBlock",
            blockHandlerClass = UserBlock.class)
    public User getUser(@PathVariable Long id) {
        return new User(id, "源码时代", "Java简单易学,端口号：" + port
                + "value:" + value);
    }
}
